const domain = "http://127.0.0.1:8088/";

window.app = {
    cookieDomain: "127.0.0.1",

    getCookie: function (cname) {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
                if (c.indexOf(name) != -1){
                    return c.substring(name.length, c.length);
                }
            }
        return "";
    },

	getUserInfo:function(){
		var userCookie = this.getCookie("user");
		if(userCookie != null && userCookie != undefined && userCookie != ''){
			var userInfoStr = decodeURIComponent(userCookie);
			if(userInfoStr != null && userInfoStr != undefined && userInfoStr != ''){
				var userInfo = JSON.parse(userInfoStr);
				if (typeof(userInfo) == "object"){
					return userInfo;

				}else{
					return null;

				}
			}

		}else{
			top.toastr.error("缓存失效，即将在5s后推出本此登录");
			setInterval(function(){
				window.location.href = "login.html";
			},5000);
		}
	},

    setCookie: function(name, value) {
        var Days = 365;
        var exp = new Date();
        exp.setTime(exp.getTime() + Days*24*60*60*1000);
        var cookieContent = name + "="+ encodeURIComponent (value) + ";path=/;";
        if (this.cookieDomain != null && this.cookieDomain != undefined && this.cookieDomain != '') {
            cookieContent += "domain=" + this.cookieDomain;
        }
        document.cookie = cookieContent + cookieContent;
    },


    deleteCookie: function(name) {
        document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
    },

	permission: function(menuCode){
		var url = 'roleAuth/getRolePermission';
		var result;
		jQuery.ajax({
			url:domain + url,
			type:"get",
			data:{},
			async: false,
			crossDomain:true,
			xhrFields:{
				withCredentials:true
			},
			beforeSend:function(xhr){
				xhr.setRequestHeader("menuCode",menuCode);
			},
			success:function (data) {
				if(data.status == 200){
					result = data.data;
				}else{
                    top.toastr.warning(data.msg);
				}
			},
			error:function(data) {
				top.toastr.error("查询列表失败，请检查网络");
			}
		})
		return result;
	},
	menuButtonByOn: function(menuCode){
		var url = 'roleAuth/getMenuPermission';
		var result;
		jQuery.ajax({
			url:domain + url,
			type:"get",
			data:{},
			async: false,
			crossDomain:true,
			xhrFields:{
				withCredentials:true
			},
			beforeSend:function(xhr){
				xhr.setRequestHeader("menuCode",menuCode);
			},
			success:function (data) {
				if(data.status == 200){
					result = data.data;
				}else{
                    top.toastr.warning(data.msg);
				}
			},
			error:function(data) {
				top.toastr.error("查询列表失败，请检查网络");
			}
		})
		return result;
	},
	//从缓存中获取用户信息，判断用户是否登录
	isUserTokenRight: function (){
		let hasRightToken = false;
		const userCookie = app.getCookie("user");
		if(userCookie==""){
			return hasRightToken;
		}
		const userInfo = app.getUserInfo();
		if(userInfo == null || userInfo == undefined || userInfo == ''){
			return hasRightToken;
		}
		const url = 'sysUser/isUserTokenRight';
		jQuery.ajax({
			url:domain + url,
			type:"post",
			data:{},
			crossDomain:true,
			async:false,
			xhrFields:{
				withCredentials:true
			},
			success:function (data) {
				if(data.status == 200){
					hasRightToken = true;
				}else {
					hasRightToken = false;
				}
			},
			error:function(data) {
				hasRightToken = false;
			}
		})
		return hasRightToken;
	}

}

// 交集
function intersection(arr1,arr2){
	return arr1.filter(function (val) { return arr2.indexOf(val) > -1 });
}


// 并集
function union(arr1,arr2){
	return arr1.concat(arr2.filter(function (val) { return !(arr1.indexOf(val) > -1) }))
}

// 补集 两个数组各自没有的集合
function complement(arr1,arr2){
	return arr1.filter(function (val) { return !(arr2.indexOf(val) > -1) })
	.concat(arr2.filter(function (val) { return !(arr1.indexOf(val) > -1) }));
}
// 差集 数组arr1相对于arr2所没有的
function diff(arr1,arr2){
	return arr1.filter(function (val) { return arr2.indexOf(val) === -1 });
}

function drawBtn(roleAuth,menuAuth){
	var hiddenBtn = "";
	let complementStr = complement(roleAuth,menuAuth);
	for(var i=0;i<complementStr.length;i++){
		hiddenBtn += "."+complementStr[i];
		if(i<complementStr.length-1){
			hiddenBtn += ",";
		}
	}
	if(!!hiddenBtn){
		hiddenBtn += "{display:none;}";
	}
	$("<style></style>").text(hiddenBtn).appendTo($("head"));
}


/*
window.onload = function(){

	var userCookie = app.getCookie("user");
	if(userCookie == null || userCookie == undefined || userCookie == ''){
		toastr.error("缓存失效，即将在5s后推出本此登录");
		setInterval(function(){
			window.location.href = "login.html";
		},5000);
	}

}*/
